package com.sssoft.Yundian.activity;

import java.util.ArrayList;
import java.util.List;

import com.sssoft.Yundian.R;
import com.sssoft.Yundian.Dao.IncomeDao;
import com.sssoft.Yundian.Dao.MyHelper;
import com.sssoft.Yundian.bean.IncomeBean;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author 我的收入页
 *
 */
public class MyIncomeActivity extends Activity {
	private MyHelper IncomeHelper;

	// 全部收入下拉表
	private List<String> list_income_date = new ArrayList<String>();
	// 支付方式下拉表
	private List<String> list_payment_method = new ArrayList<String>();
	private TextView TVList_income_date;
	private TextView TVList_payment_method;
	// 动态生成listitem
	private TextView TV_income_date;
	private TextView TV_payment_method;
	// 下拉列表
	private Spinner Spinner_income_date;
	private Spinner Spinner_payment_method;
	private ArrayAdapter<String> adapter_income_date;
	private ArrayAdapter<String> adapter_payment_method;
	// 定义底部收入款列表
	private List<IncomeBean> IncomeBeanList;

	private TextView tv_AllIncome;
	// 数据库增删改查
	private IncomeDao incomeDao;
	// 数据库
	private MyHelper income_helper;
	// 总收入
	private String all_income;
	// 底部list视图
	private ListView lv_buttom;

	private TextView tv_OrdersNum;
	private TextView tv_OrdersState;
	private TextView tv_OrdersMethod;
	private TextView tv_OrdersDate;
	private TextView tv_OrdersACtualPrice;

	private String ord_num;
	private String ord_state;
	private String ord_date;
	private String ord_method;
	private String ord_total_price;
	private String ord_actual_price;
	// private BaseAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_income);
		incomeDao = new IncomeDao(this);
		// 我的收入list
		IncomeBeanList = incomeDao.queryAll();
		// 订单明细list
		all_income = incomeDao.income_all();

		lv_buttom = (ListView) findViewById(R.id.accountLV);
		tv_AllIncome = (TextView) findViewById(R.id.tv_AllIncome);
		tv_AllIncome.setText(all_income);
		// Toast.makeText(this,""+all_income+"", Toast.LENGTH_SHORT).show();
		// Log.v("1", "1");

		// 顶部收入选择收入排序方式
		list_income_date.add("全部收入  ");
		list_income_date.add("今日收入");
		list_income_date.add("昨日收入");
		list_income_date.add("近一周收入");
		list_payment_method.add("全部支付方式");
		list_payment_method.add("现金");
		list_payment_method.add("支付宝");
		list_payment_method.add("微信");
		list_payment_method.add("银联卡");
		list_payment_method.add("储值卡");

		// 下拉排序表
		Spinner_income_date = (Spinner) findViewById(R.id.sp_income_date);
		Spinner_payment_method = (Spinner) findViewById(R.id.sp_payment_method);
		adapter_income_date = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list_income_date);
		adapter_payment_method = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
				list_payment_method);
		// 为适配器设置下拉菜单样式
		adapter_income_date.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		adapter_payment_method.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// 建立适配器,sipnner控件建立
		Spinner_income_date.setAdapter(adapter_income_date);
		Spinner_payment_method.setAdapter(adapter_payment_method);

		// 下拉列表设置点击事件，选择底部的listview排序方式
		// 选择收入排序方式:按时间
		Spinner_income_date.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {// 选择item的选择点击监听事件
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				// 将所选mySpinner 的值带入myTextView 中

				if (arg2 == 0) {

				} // 文本说明
					// 根据选择列表项进行排序sort
				else if (arg2 == 1) {

					Toast.makeText(MyIncomeActivity.this, "2", Toast.LENGTH_SHORT).show();
				} else if (arg2 == 2) {
					Toast.makeText(MyIncomeActivity.this, "3", Toast.LENGTH_SHORT).show();
				} else if (arg2 == 3) {
					Toast.makeText(MyIncomeActivity.this, "4", Toast.LENGTH_SHORT).show();
				}

			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				TVList_income_date.setText("Nothing");
			}
		});
		// 按支付方式排序
		Spinner_payment_method.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {// 选择item的选择点击监听事件
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				// 将所选mySpinner 的值带入myTextView 中

				if (arg2 == 0) {
				} // 文本说明
					// 根据选择列表项进行排序sort
				else if (arg2 == 1) {

					Toast.makeText(MyIncomeActivity.this, "2", Toast.LENGTH_SHORT).show();
				} else if (arg2 == 2) {
					Toast.makeText(MyIncomeActivity.this, "3", Toast.LENGTH_SHORT).show();
				} else if (arg2 == 3) {
					Toast.makeText(MyIncomeActivity.this, "4", Toast.LENGTH_SHORT).show();
				} else if (arg2 == 4) {
					Toast.makeText(MyIncomeActivity.this, "5", Toast.LENGTH_SHORT).show();
				}

			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				TVList_income_date.setText("Nothing");
			}
		});

		// 定义适配器,存数据到listview
		BaseAdapter adapter = new BaseAdapter() {
			public int getcount() {
				return IncomeBeanList.size();
			}

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				View view = convertView != null ? convertView
						: View.inflate(getApplicationContext(), R.layout.listitem_myincome, null);
				tv_OrdersNum = (TextView) view.findViewById(R.id.TV_OrdersNum);
				tv_OrdersState = (TextView) view.findViewById(R.id.TV_OrdersState);
				tv_OrdersMethod = (TextView) view.findViewById(R.id.TV_OrdersMethod);
				tv_OrdersDate = (TextView) view.findViewById(R.id.TV_OrdersDate);
				tv_OrdersACtualPrice = (TextView) view.findViewById(R.id.TV_OrdersACtualPrice);
				IncomeBean ib = IncomeBeanList.get(position);
				tv_OrdersNum.setText(ib.getOrders_number());
				tv_OrdersState.setText(ib.getOrders_state());
				tv_OrdersMethod.setText(ib.getOrders_method());
				// String orders_date = ib.getOrders_date();
				// SimpleDateFormat dateFormater = new
				// SimpleDateFormat("yyyy-MM-dd");
				tv_OrdersDate.setText(ib.getOrders_date());
				tv_OrdersACtualPrice.setText("￥" + ib.getOrders_actual_price());
				return view;
			}

			@Override
			public Object getItem(int position) {
				return null;
			}

			@Override
			public long getItemId(int position) {
				return position;
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see android.widget.Adapter#getCount()
			 */
			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return IncomeBeanList.size();
			}
		};
		lv_buttom.setAdapter(adapter);
		lv_buttom.setOnItemClickListener(new MyOnItemClickListener());
	}

	// listview点击响应事件,跳转订单详情页
	private class MyOnItemClickListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

			IncomeBean p = (IncomeBean) parent.getItemAtPosition(position);
			// 获取当前点击item的值
			IncomeBean ib = IncomeBeanList.get(position);
			ord_num = ib.getOrders_number();
			ord_state = ib.getOrders_state();
			ord_date = ib.getOrders_date();
			ord_method = ib.getOrders_method();
			ord_total_price = ib.getOrders_total_price();
			ord_actual_price = ib.getOrders_actual_price();
			Intent intent1 = new Intent();
			intent1.putExtra("OrdersNum", ord_num);
			intent1.putExtra("OrdersState", ord_state);
			intent1.putExtra("OrdersDate", ord_date);
			intent1.putExtra("OrdersMethod", ord_method);
			intent1.putExtra("OrdersTotalPrice", ord_total_price);
			intent1.putExtra("OrdersActualPrice", ord_actual_price);
			intent1.setClass(MyIncomeActivity.this, OrdersDetailsActivity.class);
			startActivity(intent1);
			// Toast.makeText(getApplicationContext(),""+ord_state+"",
			// Toast.LENGTH_SHORT).show();

		}

	}

}

